/*
 * @(#)CheckAndCreateOfflineTask.java   1.0  2021年11月18日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.init;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.swift.core.env.ConfigInterface.ChangeConfigKV;
import com.swift.core.env.ConfigInterface.ConfigChangeListener;
import com.swift.dts.dao.DbConfigDao;
import com.swift.dts.dao.OfflineStateInfoDao;
import com.swift.dts.dao.TablesDao;
import com.swift.dts.dao.model.OfflineStateInfo;
import com.swift.dts.dao.model.SynObjectKey;
import com.swift.dts.init.core.TableChange;
import com.swift.dts.loadconfig.model.DtsDataBase;
import com.swift.dts.loadconfig.model.TablesConfig;
import com.swift.dts.model.Table;
import com.swift.dts.offline.OfflineLifeCycle;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月18日
 */
@Component
@Order
public class OfflineTaskTableChange extends TableChange implements ConfigChangeListener<TablesConfig> {
    @Autowired
    private OfflineLifeCycle offlineLifeCycle;
    
    @Autowired
    private DbConfigDao dbConfigDao;
    
    @Autowired
    private TablesDao tablesDao;
    
    @Autowired
    private OfflineStateInfoDao offlineStateInfoDao;
    /** 
     * @see com.swift.core.env.ConfigInterface.ConfigChangeListener#listener(java.util.List)
     */
    @Override
    public void listener(List<ChangeConfigKV<TablesConfig>> list) {
        if(!checkTableFilterChange(list)) return;
        List<String> dbSourceList = getSourceKey(true);
        List<String> dbSynList = getSourceKey(false);
        if(TypeUtil.isNull(dbSourceList)) return;
        if(TypeUtil.isNull(dbSynList)) return;
        
        for(String dbKey:dbSourceList) {
            for(String synDb:dbSynList) {
                deleteNotSynTable(dbKey,synDb);
                offlineLifeCycle.startOfflineTash(dbKey, synDb);
            }
        }
    }
    

    //删除不再需要同步的表
    private void deleteNotSynTable(String dataSource,String destination) {
        //找出所有不需要同步的表
        List<Table> notSynTables = tablesDao.getAllNotSynTables(dataSource);
        if(TypeUtil.isNull(notSynTables)) return;
        //删除离线同步进度数据
        List<OfflineStateInfo> offList = offlineStateInfoDao.list();
        if(TypeUtil.isNull(offList)) return;
        for(Table table:notSynTables) {
            for(OfflineStateInfo offTable:offList) {
                if(isEqual(dataSource, destination, table, offTable)) {
                    SynObjectKey key =  createKey(dataSource, destination, table);
                    offlineLifeCycle.delete(key);
                    offlineStateInfoDao.delete(key);
                }
            }
        }
    }
    
    private boolean isEqual(String dataSource,String destination,Table table,OfflineStateInfo offTable) {
        if(!dataSource.equalsIgnoreCase(offTable.getDataSource())) return false;
        if(!destination.equalsIgnoreCase(offTable.getDestination())) return false;
        if(!table.getDbName().equalsIgnoreCase(offTable.getDbName())) return false;
        if(!table.getTableName().equalsIgnoreCase(offTable.getTableName())) return false;
        return true;
    }
    
    private SynObjectKey createKey(String dataSource,String destination,Table table) {
        SynObjectKey key = new SynObjectKey();
        key.setDataSource(dataSource);
        key.setDbName(table.getDbName());
        key.setDestination(destination);
        key.setTableName(table.getTableName());
        return key;
    }
    
    private List<String> getSourceKey(boolean isSource){
        List<String> list = new LinkedList<String>();
        List<DtsDataBase> dbList = dbConfigDao.list();
        if(TypeUtil.isNull(dbList)) return null;
        for(DtsDataBase db:dbList) {
            if(db.getIsSource()==isSource) {
                list.add(db.getKey());
            }
        }
        return list;
    }
    
}
